﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Controls;
using CoreData;
using Helper;

namespace SchoolManagementSystem.Details
{
    public partial class ProductUC : DetailUC
    {
        public ProductUC()
        {
            InitializeComponent();
            this.ObjectName = "sản phẩm";
            this.Code = "Product";
            this.Type = new Product();

            comboboxUC_ProductGroup.Model = RegisterService.RegisterServices.ProductGroupService;
            comboboxUC_ProductGroup.Uc = new Product_GroupUC();
            comboboxUC_ProductGroup.ColumnMain = "Name";
            comboboxUC_ProductGroup.ColumnDisplay = "Code,Name";

            comboboxUC_Provider.Model = RegisterService.RegisterServices.ProductProviderService;
            comboboxUC_Provider.Uc = new Product_ProviderUC();
            comboboxUC_Provider.ColumnMain = "Name";
            comboboxUC_Provider.ColumnDisplay = "Code,Name";

            //comboboxUC_StockUnit.Model = RegisterService.RegisterServices.ProductUnitService;
            //comboboxUC_StockUnit.Uc = new Product_UnitUC();
            //comboboxUC_StockUnit.ColumnMain = "Name";
            //comboboxUC_StockUnit.ColumnDisplay = "Code,Name";

            comboboxUC_InputUnit.Model = RegisterService.RegisterServices.ProductUnitService;
            comboboxUC_InputUnit.Uc = new Product_UnitUC();
            comboboxUC_InputUnit.ColumnMain = "Name";
            comboboxUC_InputUnit.ColumnDisplay = "Code,Name";

            //comboboxUC_OutPutUnit.Model = RegisterService.RegisterServices.ProductUnitService;
            //comboboxUC_OutPutUnit.Uc = new Product_UnitUC();
            //comboboxUC_OutPutUnit.ColumnMain = "Name";
            //comboboxUC_OutPutUnit.ColumnDisplay = "Code,Name";


            //masterMainUC1.Model = RegisterService.RegisterServices.ProductUnitChangeService;
            //masterMainUC1.DetailUCControl = new Product_UnitChangeUC();
            //masterMainUC_Atrr.Model = RegisterService.RegisterServices.Product_AttributeService;
            //masterMainUC_Atrr.DetailUCControl = new Product_AttributeUC();

            
            //Product p = new Product();
            //if (p.ID > 0)
            //{
            //    xtraTabPage_UnitChange.PageEnabled = true;
            //    xtraTabPage_Attribute.PageEnabled = true;
            //}
            //else
            //{ 
            //    xtraTabPage_UnitChange.PageEnabled = false;
            //    xtraTabPage_Attribute.PageEnabled = false;
            //}
        //
        //comboboxUC_ProductGroup.combobox.EditValueChanged += new EventHandler(comboboxUC_ProductGroup_EditValueChanged);
        }
        void comboboxUC_ProductGroup_EditValueChanged(object sender, EventArgs e)
        {
            //string code = (string)RegisterService.RegisterServices.ProductService.GetProductCodeByGroup(comboboxUC_ProductGroup.ID);
            //txtcode.Text = code;
        }
        //int landau = 0;
        String ProductID = "1=1";
        protected override long DetailDisplay()
        {
            
            Product data = new Product();
            if (ID > 0)
            {
                data = (Product)RegisterService.RegisterServices.ProductService.GetByID(ID);
                ProductID = data.ID.ToString();
                //if (landau == 1)
                //{
                //    xtraTabPage_UnitChange.PageEnabled = true;//Hien thi tab qui doi don vi khi chon Product tu grid
                //    xtraTabPage_Attribute.PageEnabled = true;
                //}
                //landau = 1;
            }

            txtname.Text = data.Name;
            txtnote.Text = data.Description;
            txtcode.Text = data.Code;
            comboboxUC_ProductGroup.ID = data.Product_GroupID;
            comboboxUC_Provider.ID = data.Product_ProviderID;
            comboboxUC_InputUnit.ID = data.Input_UnitID;
            //comboboxUC_OutPutUnit.ID = data.Output_UnitID;
           // comboboxUC_StockUnit.ID = data.Stock_UnitID;
            spinEdit_MoneyIN.EditValue = data.Input_Price;
            //spinEdit_MoneyOut.EditValue = data.Output_Price;
            spinedit_mintock.EditValue = data.Stock_Min;
            spinedit_maxock.EditValue = data.Stock_Max;
            ckactive.Checked = data.Active;
            var datainVoucherDetail=RegisterService.RegisterServices.StockVoucherInDetailService.GetByProductID(ID);
            if (datainVoucherDetail != null)
            {

                comboboxUC_InputUnit.Enabled = false;
                //comboboxUC_OutPutUnit.Enabled = false;
                //comboboxUC_StockUnit.Enabled = false;

            }
            //masterMainUC1.SearchString = "ProductID=" + ProductID;
            //masterMainUC1.DetailUCControl.SearchString = "ProductID=" + ProductID;
            //masterMainUC_Atrr.SearchString = "ProductID=" + ProductID;
            //masterMainUC_Atrr.DetailUCControl.SearchString = "ProductID=" + ProductID;
            return ID;

        }

        protected override long Save()
        {
            Product data = new Product();
            if (ID > 0)
            {
                data = (Product)RegisterService.RegisterServices.ProductService.GetByID(ID);
                //xtraTabPage_UnitChange.PageEnabled = true;//Hien thi tab qui doi don vi khi them moi
                //xtraTabPage_Attribute.PageEnabled = true;
            }
            long tempID = ID;//luu trang thai form tam
            data.Code = txtcode.Text; 
            data.Name = txtname.Text;
            data.Description = txtnote.Text;
            data.Active = ckactive.Checked;
            
            data.Stock_Min = int.Parse(spinedit_mintock.EditValue.ToString());
            data.Stock_Max = int.Parse(spinedit_maxock.EditValue.ToString());
            data.Product_ProviderID = comboboxUC_Provider.ID;
            data.Product_GroupID = comboboxUC_ProductGroup.ID;           
            //data.Output_Price = int.Parse(spinEdit_MoneyOut.EditValue.ToString());
            data.Input_UnitID= comboboxUC_InputUnit.ID;
            data.Output_UnitID =comboboxUC_InputUnit.ID;
            data.Stock_UnitID = comboboxUC_InputUnit.ID;
            data.Input_Price = int.Parse(spinEdit_MoneyIN.EditValue.ToString());

            ID = RegisterService.RegisterServices.ProductService.Save(data, ref ErrorMess);
            
            //if (tempID <= 0)//Them bang quy doi don vi mac dinh cho sp
            //{
            //    Product_UnitChange data_UnitChange = new Product_UnitChange();
            //    data_UnitChange.ProductID = ID;
            //    data_UnitChange.UnitID = comboboxUC_InputUnit.ID;
            //    data_UnitChange.UnitChangeID = comboboxUC_InputUnit.ID;
            //    data_UnitChange.Ratio = 1;
            //    RegisterService.RegisterServices.ProductUnitChangeService.Save(data_UnitChange);
            //}
            //masterMainUC1.DetailUCControl.SearchString = ID.ToString();
            //masterMainUC_Atrr.DetailUCControl.SearchString = ID.ToString();
            return ID;
        }


        public override bool Delete(long SelectedID)
        {
            var data=RegisterService.RegisterServices.StockVoucherInDetailService.GetByProductID(SelectedID);
            if (data.ProductID > 0)
            {
                MessageBox.Show("Không thể xóa mặt hàng này. Hàng hóa nằm trong danh mục lưu kho", "Xóa dữ liệu", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else
            return RegisterService.RegisterServices.ProductService.Delete(RegisterService.RegisterServices.ProductService.GetByID(SelectedID));

        }

        private void ProductUC_Load(object sender, EventArgs e)
        {
            AddTextChanged(xtraTabPage_Info.Controls);
        }
    }
}
